#coding=utf-8
import requests
import F
import arcpy
import json
import time
arcpy.env.overwriteOutput = True
# countyList = ['宝兴县','汉源县','芦山县','名山区','石棉县','天全县','荥经县','雨城区']
countyList = ['宝兴县']
url = "https://restapi.amap.com/v5/place/text"
cnList = []
for county in countyList:
    county_de = county.decode('utf-8')
    page_num = 1
    while True:
        params = {
            "key": "e46084ea3a1848ea0c135392490152a4",
            "keywords": "工厂".decode('utf-8'),
            "region": county_de,
            "page_num" : page_num,
            "page_size": 25,
        }
        response = requests.get(url, params=params)
        data = json.loads(response.text)
        if not data['pois']:
            break

        dataCount = len(data['pois'])
        print county,dataCount,"当前页:%s"%page_num
        for i in range(dataCount):
            name = data['pois'][i]['name']
            county = data['pois'][i]['adname']
            address = data['pois'][i]['address']
            p_x = float(data['pois'][i]['location'].split(',')[0])
            p_y = float(data['pois'][i]['location'].split(',')[1])
            p_x_trans = F.gcj02_to_wgs84(p_x,p_y)[0]
            p_y_trans = F.gcj02_to_wgs84(p_x,p_y)[1]
            cnList.append( [p_x_trans,p_y_trans,name,county,address])
        page_num += 1
        time.sleep(4)
p = arcpy.CreateFeatureclass_management(r'e:\test','test','POINT')
arcpy.AddField_management(p,'name','TEXT')
arcpy.AddField_management(p,'county','TEXT')
arcpy.AddField_management(p,'address','TEXT')
yb = arcpy.da.InsertCursor(p,['SHAPE@X','SHAPE@Y','name','county','address'])
for cn in cnList:
    yb.insertRow(cn)
del yb
